﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml;

namespace Demo.SharePoint.WebParts.StockQuote
{
    public class StockQuoteReader
    {
        private XmlDocument stockQuotes;

        /// <summary>
        /// Initializes a new instance of the <see cref="StockQuoteReader"/> class.
        /// </summary>
        public StockQuoteReader()
        {
            stockQuotes = new XmlDocument();
        }

        /// <summary>
        /// Retrieves the stock details for the specified stock symbols.
        /// </summary>
        /// <param name="stockSymbols">The stock symbols.</param>
        public void LoadStockQuotes(IList<string> stockSymbols)
        {
            string symbols = String.Empty;
            foreach (string symbol in stockSymbols)
                symbols += symbol.Trim() + ",";
            symbols = symbols.TrimEnd(new[] { ',' });

            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(String.Format("http://office.microsoft.com/Research/Providers/MoneyCentral.asmx/DDSQuery?symbol={0}&language=en-us&iSrc=", symbols));
            HttpWebResponse webResponse = (HttpWebResponse)webReq.GetResponse();

            // Get the stream associated with the response.
            Stream receiveStream = webResponse.GetResponseStream();

            // Read xml into xml doc
            // Note: The xml is wrapped in one node: <string xmlns="http://Office.Microsoft.Com/Research"> and 
            // the xml we want is a text value inside this node.
            XmlReader xmlReader = XmlReader.Create(receiveStream);
            stockQuotes.Load(xmlReader);
            stockQuotes.LoadXml(stockQuotes.DocumentElement.InnerText);

            webResponse.Close();
        }

        /// <summary>
        /// Gets the stock quote.
        /// </summary>
        /// <param name="symbol">The symbol.</param>
        /// <returns></returns>
        public StockQuote GetStockQuote(string symbol)
        {
            if (stockQuotes.DocumentElement == null)
                return null;

            XmlNode xmlQuote = stockQuotes.DocumentElement.SelectSingleNode(String.Format("descendant::Quote[@Symbol='{0}']", symbol.ToUpper()))
                               ?? stockQuotes.DocumentElement.SelectSingleNode(String.Format("descendant::Quote[@Symbol='{0}']", symbol.ToLower()));

            if (xmlQuote == null)
                return null;

            StockQuote quote = new StockQuote(symbol.ToUpper());

            XmlNode node = xmlQuote.SelectSingleNode("descendant::Last");
            quote.Last = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::LastTime");
            quote.LastTime = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::LastSize");
            quote.LastSize = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::Ask");
            quote.Ask = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::Bid");
            quote.Bid = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::Open");
            quote.Open = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::Close");
            quote.Close = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::High");
            quote.High = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::Low");
            quote.Low = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::Volume");
            quote.Volume = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::Change");
            quote.Change = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::PercentChange");
            quote.PercentChange = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::EPS");
            quote.EPS = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::PE");
            quote.PE = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::SharesOut");
            quote.SharesOut = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::Currency");
            quote.Currency = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::Exchange");
            quote.Exchange = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::Type");
            quote.Type = node == null ? String.Empty : node.InnerText;
            node = xmlQuote.SelectSingleNode("descendant::CompanyName");
            quote.CompanyName = node == null ? String.Empty : node.InnerText;

            return quote;
        }
    }
}